package
{
	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-29上午01:12:00
	 * */
	import event.UIEditsEventName;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	import kingsAsLib.utils.KClearDisplay;
	import kingsAsLib.displays.ReferenceWin;
	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;
	
	import layer.LayerManagePan;

	public class UIView extends Sprite
	{
		static private var uiView:UIView=new UIView();

		private var bgLayer:Sprite;

		private var _contenLayer:Sprite;

		private var currentLayer:Sprite;

		private var addObjList:Array=new Array();


		//移动框
		private var uiMoveBox:ReferenceWin=new ReferenceWin();

		public function UIView()
		{
			this.addEventListener(Event.ADDED_TO_STAGE, inits);

		}

		protected function inits(event:Event):void
		{
			bgLayer=new Sprite();
			addChild(bgLayer);

			contenLayer=new Sprite();
			contenLayer.name="contenLayer";
			addChild(contenLayer);

			currentLayer=new Sprite();
			currentLayer.name="oneLayer"


			addLayer(currentLayer);

			listener();

		}

		private function listener():void
		{
			EventManager.addEventListener(UIEditsEventName.classItemClick, chooseUI);
			EventManager.addEventListener(UIEditsEventName.CurrentLayerChange, currentLayerChangeHandle);

			EventManager.addEventListener(UIEditsEventName.addLayer, addLayerHandle);
			EventManager.addEventListener(UIEditsEventName.lockLayer, lockLayerHandle);
			EventManager.addEventListener(UIEditsEventName.deletLayer, deleLayerHandle);
		}

		private function lockLayerHandle(e:KEvents):void
		{
			currentLayer.mouseEnabled=!currentLayer.mouseEnabled;
			currentLayer.mouseChildren=!currentLayer.mouseChildren;
			EventManager.dispatchEvent(new KEvents(UIEditsEventName.CurrentLayerChange, currentLayer));
		}

		private function deleLayerHandle(e:KEvents):void
		{
			// 
			deleteLayer();
		}

		private function addLayerHandle(e:KEvents):void
		{
			// 
			addLayer(new Sprite);
		}

		private function currentLayerChangeHandle(e:KEvents):void
		{
			// 
			currentLayer=e.getData();
		}

		private function chooseUI(e:KEvents):void
		{
			var className:String=e.getData() as String;
			if (className)
			{

				var cls:Class=getClass(className);
				if (cls)
				{
					if (new cls as DisplayObject)
					{
						showUI(new cls as DisplayObject);
					}
				}
			}

		}

		private function showUI(obj:DisplayObject):void
		{
			if (obj)
			{
				addObjList.push(obj);
				currentLayer.addChild(obj);

			}

			resetObjListener();
		}

		private function resetObjListener():void
		{
			// 
			for each (var i:DisplayObject in addObjList)
			{

				i.addEventListener(MouseEvent.MOUSE_DOWN, moveObjHandle);

				i.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardHandle);
			}

		}

		protected function KeyboardHandle(e:KeyboardEvent):void
		{
			if (e.keyCode == Keyboard.DELETE)
			{
				var dis:DisplayObject=e.target as DisplayObject;
				if (dis)
				{
					KClearDisplay.RemoveFromParent(dis);
				}
			}

		}

		protected function UIMouseUPHandle(dis:DisplayObject):void
		{
			dis.x=uiMoveBox.targetXY().x;
			dis.y=uiMoveBox.targetXY().y;

			removeChild(uiMoveBox);
			uiMoveBox.destory();
		}

		protected function moveObjHandle(e:Event):void
		{
			var dis:DisplayObject=e.currentTarget as DisplayObject;
			dis=getUI(dis);
			if (dis)
			{
				uiMoveBox.start(this, dis, UIMouseUPHandle);
				uiMoveBox.x=dis.x;
				uiMoveBox.y=dis.y;
				addChild(uiMoveBox);

				EventManager.dispatchEvent(new KEvents(UIEditsEventName.chooseObj, dis));
			}

		}

		private function getUI(dis:DisplayObject):DisplayObject
		{

			var clsName:String=getQualifiedClassName(dis.parent);
			var index:int=clsName.indexOf("::")
			var tempStr:String=clsName.substr(index + 2);
			if (tempStr != "Sprite")
			{
				getUI(dis.parent);
			}

			return dis;
		}

		private function getClass(name:String):Class
		{
			try
			{
				var ClassReference:Class=getDefinitionByName(name) as Class;
			}
			catch (e:Error)
			{
				trace("getClass " + name + "error" + e.message);
				return null;
			}
			return ClassReference;
		}

		static public function instance():UIView
		{
			return uiView;
		}

		public function setBgSize(_w:int=600, _h:int=500):void
		{
			bgLayer.graphics.clear();
			bgLayer.graphics.beginFill(0, 0.3);
			bgLayer.graphics.drawRect(0, 0, _w, _h);
			bgLayer.graphics.endFill();
		}
		
		public function setBgBitmap(_pic:Bitmap):void{
			KClearDisplay.RemoveEmpty(bgLayer);
			bgLayer.addChild(_pic);
		}

		public function get contenLayer():Sprite
		{
			return _contenLayer;
		}

		public function set contenLayer(value:Sprite):void
		{
			_contenLayer=value;
		}

		public function addLayer(dis:Sprite):void
		{
			LayerManagePan.instance().addLayer(dis);
			contenLayer.addChild(dis);
			currentLayer=dis;
		}

		public function deleteLayer():void
		{
			LayerManagePan.instance().removeLayer(currentLayer);
			contenLayer.removeChild(currentLayer);

		}

	}

	//end class
}
